package com.itheima.jdbc2;

import com.itheima.utils.JDBCUtils1;

import java.sql.*;

/*
    数据库：查询数据
 */
public class demo4_1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        // 获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db4", "root", "root");

        // 获取语句执行对象
        Statement statement = connection.createStatement();

        // 编写SQL
        String sql = "select * from user";

        // 执行SQL
        ResultSet resultSet = statement.executeQuery(sql);

        // 处理结果集
        while (resultSet.next()) {
            /*
                获取每一行数据
                    rs.getXxx("列名");    Xxx类型和数据库列类型一致
             */
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            System.out.println(id + "," + name + "," + age);
        }

        JDBCUtils1.release(resultSet,statement,connection);
    }
}
